﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using System.Diagnostics;

namespace babynames
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private Parser _parser;

        public MainWindow()
        {
            InitializeComponent();
            _parser = new Parser("04-babynames.txt");
        }

        private void MainWindowLoaded(object sender, RoutedEventArgs e)
        {
            for (int i = 1900; i <= 2000; i+=10 )
                DecadeListBox.Items.Add(i);
        }

        private void ShowNamesForDecade(int decade)
        {
            top10ListBox.ItemsSource = _parser.TopTenBabyNames(decade);
        }

        private void SearchButton_Click(object sender, RoutedEventArgs e)
        {
            int AvgRank = 0, Trend = 0;
            RankYearBox.ItemsSource = _parser.BabySearchQuery(SearchBox.Text, out AvgRank, out Trend);
            TrendBox.Text = Trend.ToString();
            AvgRankBox.Text = AvgRank.ToString();
        }

        private void top10ListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                SearchBox.Text = e.AddedItems[0].ToString();
                if (StatusLabel.Content == "Some none-fatal error happend..")
                    StatusLabel.Content = "";
            }
            catch(Exception)
            {
                //HACKSLOL
                StatusLabel.Content = "Some none-fatal error happend..";
            }

            SearchButton_Click(null,null);
        }

        private void DecadeListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ShowNamesForDecade(Convert.ToInt32(e.AddedItems[0]));
        }

        private void Quit_Clicked(object sender, RoutedEventArgs e)
        {
            Application.Current.Shutdown();
        }

        private void Small_Clicked(object sender, RoutedEventArgs e)
        {
            GlobalWindow.FontSize = 8;
        }

        private void Normal_Clicked(object sender, RoutedEventArgs e)
        {
            GlobalWindow.FontSize = 12;
        }


        private void Large_Clicked(object sender, RoutedEventArgs e)
        {
            GlobalWindow.FontSize = 16;
        }

        private void Huge_Clicked(object sender, RoutedEventArgs e)
        {
            GlobalWindow.FontSize = 23;
        }

    }
}
